package com.slmxz.tms.document.controller;

import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.document.model.entity.Location;
import com.slmxz.tms.document.model.vo.LocationVO;
import com.slmxz.tms.document.service.LocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 地址档案前端控制层
 * @author sunyao
 * @Date 2024-11-15
 */
@Tag(name = "地址档案接口")
@RestController
@RequestMapping("/document/location")
@RequiredArgsConstructor
public class LocationController  {

    private final LocationService locationService;

    @Operation(summary = "地址档案分页列表")
    @GetMapping("/page")
    public PageResult<LocationVO> selectLocationPage(Query query, LocationVO location) {
        IPage<LocationVO> result = this.locationService
                .selectLocationPage(Condition.getPage(query),location);
        return PageResult.success(result);
    }


    @Operation(summary = "地址信息的下拉框")
    @GetMapping("/selectOption")
    public Result<?> selectOption(LocationVO location){
        return Result.success(this.locationService.selectOption(location));
    }


    @Operation(summary = "新增地址档案")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('document:location:add')")
    public Result<Void> saveLocation(@RequestBody @Valid LocationVO  location) {
        boolean result = this.locationService.saveLocation(location);
        return Result.result(result);
    }

    @Operation(summary = "获取地址档案详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('document:location:edit')")
    public Result<Location> selectLocationDetail(
        @Parameter(description = "地址档案ID") @PathVariable Long id
    ) {
        Location location = this.locationService.selectLocationDetail(id);
        return Result.success(location);
    }

    @Operation(summary = "修改地址档案")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('document:location:edit')")
    public Result<Void> updateLocation(
            @RequestBody @Validated Location location
    ) {
        boolean result = locationService.updateLocation(location);
        return Result.result(result);
    }

    @Operation(summary = "删除地址档案")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('document:location:delete')")
    public Result<Void> deleteLocations(
        @Parameter(description = "地址档案ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.locationService.deleteLocations(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销地址档案")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody LocationVO location) {
        boolean result = this.locationService.activeOrLogout(location);
        return Result.result(result);
    }
}
